package com.study.fun;

import cn.hutool.core.util.RandomUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.List;
import java.util.function.BiConsumer;

/**
 * @author zhuzi
 * 2024/3/31 14:17
 */
public class Demo {
    public static void copySelectiveProperties(Object source, Object target, List<String> propertiesToCopy) {
        BeanWrapper srcWrapper = new BeanWrapperImpl(source);
        BeanWrapper targetWrapper = new BeanWrapperImpl(target);

        for (String property : propertiesToCopy) {
            if (srcWrapper.isReadableProperty(property) && targetWrapper.isWritableProperty(property)) {
                targetWrapper.setPropertyValue(property, srcWrapper.getPropertyValue(property));
            }
        }
    }

    @Test
    public void setFun() {
        A a = new A().setName(RandomUtil.randomString(5)).setAge(RandomUtil.randomInt(5, 18)).setSex(RandomUtil.randomString(1));
        B b = new B();
        BeanUtils.copyProperties(a, b, "c");


        a.setName("哈哈");
        B b2 = new B();
        copySelectiveProperties(a, b2, Lists.newArrayList("name"));
        System.out.println(b2);


    }

    public static class SetGetFun {
        private Object objVal;
        private BiConsumer consumer;

        public static void getSet(Object val, BiConsumer consumer, Object oj) {
            consumer.accept(oj, val);
        }
    }

    @Data
    @Accessors(chain = true)
    public static class A {
        private String name;
        private Integer age;
        private String sex;
    }

    @Data
    @Accessors(chain = true)
    public static class B {
        private String name;
        private Integer age;
        private String sex;
    }


}
